package com.x.erp.manage;

import com.x.erp.dao.MultilevelActivityBondDao;
import com.x.erp.model.MultilevelActivity;
import com.x.erp.model.MultilevelActivityBond;
import com.x.framework.manage.BaseManage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class MultilevelActivityManage extends BaseManage{

	@Autowired
	private MultilevelActivityBondDao multilevelActivityBondDao;// 多级营销活动保证金管理

	public MultilevelActivity insertMultilevelActivity(MultilevelActivity multilevelActivity) throws Exception {
		//根据车商ID查询保证金管理
		MultilevelActivityBond multilevelActivityBond = new MultilevelActivityBond();
		multilevelActivity.setShopId(multilevelActivity.getShopId());
		List<MultilevelActivityBond> multilevelActivityBonds = multilevelActivityBondDao.selectMultilevelActivityBondList(multilevelActivityBond);
		if(multilevelActivityBonds.isEmpty()){
			return null;
		}
		//获取到车商保证金信息
		multilevelActivityBond = multilevelActivityBonds.get(0);
		if(multilevelActivityBond.getBondMoney().compareTo(multilevelActivity.getActivityBounty()) == -1){
			return null;
		}
		//修改车商保证金
		multilevelActivityBond.setBondFreezingMoney(multilevelActivityBond.getBondFreezingMoney().add(multilevelActivity.getActivityBounty()));
		multilevelActivityBond.setBondMoney(multilevelActivityBond.getBondMoney().subtract(multilevelActivity.getActivityBounty()));
		Date cDate = this.getDate(0);
		multilevelActivityBond.setBondUpdateTime(cDate);
		this.update(multilevelActivityBond);
		return this.insert(multilevelActivity);
	}

}
